package sg.edu.nus.iss.team8s.vmcs.builder;
/**
 * 
 * @author Nie Xiaoyi
 * 
 */
import java.awt.Checkbox;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import sg.edu.nus.iss.team8s.vmcs.Listener.StoreViewerListener;
import sg.edu.nus.iss.team8s.vmcs.beans.StoreItem;
import sg.edu.nus.iss.team8s.vmcs.beans.StoreObject;
import sg.edu.nus.iss.team8s.vmcs.cache.CashStore;
import sg.edu.nus.iss.team8s.vmcs.cache.DrinksStore;
import sg.edu.nus.iss.team8s.vmcs.commons.CommonsFactory;
import sg.edu.nus.iss.team8s.vmcs.commons.Constant;
import sg.edu.nus.iss.team8s.vmcs.control.MachineryController;

public class MachineryBuilder implements UIBuilder {
	public static final String MACHINERY_PANEL_TITLE = "VMCS - Machinery Panel";
	public static final String QUANTITY_OF_COINS = "Quantity of Coins";
	public static final String CENT_5 = "5 c";
	public static final String CENT_10  = "10 c";
	public static final String CENT_20 = "20 c";
	public static final String CENT_50 = "50 c";
	public static final String CENT_100 = "$ 1";
	public static final String QUANTITY_OF_CANS = "Quantity of Cans";
	public static final String DOOR_STATUS = "Door Locked";
	public static final String ZERO = "0";
	
	private MachineryPanel machineryPanel;
	private TextField[] txtListCoinQty;
	private TextField[] txtListDrinkQty;
	
	private MachineryController machineryController;
	public MachineryBuilder(){
		machineryPanel = new MachineryPanel();
		machineryController = MachineryController.getInstance();
	}
	
	public void buildTitlePanel(){
		Panel p = new Panel();
		p.setPreferredSize(new Dimension(Constant.PANEL_WIDTH, 80));
		//p.setBackground(Color.YELLOW);
		p.setBackground(Constant.DEFAULT_BACKGROUND_COLOR);
		
		Label title = new Label(MachineryBuilder.MACHINERY_PANEL_TITLE); 
		title.setFont(new Font("Times New Roman", Font.BOLD, 20));
		p.add(title);
		
		//panelUI.add(p);
		machineryPanel.setTitlePanel(p);
		machineryPanel.add(p);
	}
	
	public void buildInputPanel(){
		int GAPLENGTH = 0;
		Panel p = new Panel();
		p.setPreferredSize(new Dimension(Constant.PANEL_WIDTH, 225));
		p.setBackground(Constant.DEFAULT_BACKGROUND_COLOR);
		//p.setBackground(Color.PINK);
		p.setLayout(null);
		
		Label quantityCoin = new Label(MachineryBuilder.QUANTITY_OF_COINS);
		quantityCoin.setPreferredSize(new Dimension(150, 20));
		quantityCoin.setFont(new Font("Times New Roman", Font.BOLD, 12));
		
		Insets insets = p.getInsets();
		Dimension d = quantityCoin.getPreferredSize();
		quantityCoin.setBounds(1 + insets.left, 0 + insets.top, d.width, d.height);
		
		p.add(quantityCoin);
		
		CashStore cashStore = CashStore.getInstance();
		txtListCoinQty = new TextField[cashStore.getStoreSize()];
		for(int i =0;i<cashStore.getStoreSize();i++){
			StoreItem item = cashStore.getItem(i);
			StoreObject obj = item.getContent();
			
			Label lblCash = new Label(obj.getName());
			lblCash.setName(obj.getClass().getName()+ String.valueOf(i));
			lblCash.setPreferredSize(new Dimension(50, 20));
			lblCash.setAlignment(Label.RIGHT);
			lblCash.setFont(new Font("Times New Roman", Font.BOLD, 12));
			
			/*Label lblqty = new Label(String.valueOf(item.getQuantity()));
			lblqty.setName(item.getClass().getName()+String.valueOf(i));
			lblqty.setPreferredSize(new Dimension(50, 20));
			lblqty.setAlignment(Label.CENTER);
			lblqty.setBackground(Color.BLACK);
			lblqty.setForeground(Color.YELLOW);
			lblqty.setFont(new Font("Monospaced", Font.PLAIN, 12));*/
			
			TextField txtQty = new TextField(String.valueOf(item.getQuantity()));
			txtQty.setName(item.getClass().getName()+String.valueOf(i));
			txtQty.setPreferredSize(new Dimension(50, 20));
			txtQty.setBackground(Color.BLACK);
			txtQty.setForeground(Color.YELLOW);
			txtQty.setFont(new Font("Monospaced", Font.PLAIN, 12));
			
			d = lblCash.getPreferredSize();
			lblCash.setBounds(120 + insets.left, 25 + GAPLENGTH + insets.top, d.width, d.height);
			
			/*d = lblqty.getPreferredSize();
			lblqty.setBounds(180 + insets.left, 25 + GAPLENGTH + insets.top, d.width, d.height);*/
			d = txtQty.getPreferredSize();
			txtQty.setBounds(180 + insets.left, 25 + GAPLENGTH + insets.top, d.width, d.height);
			
			txtQty.addTextListener(new StoreViewerListener(0,i,item.getQuantity()));
			
			txtListCoinQty[i] = txtQty;
			
			p.add(lblCash);
			//p.add(lblqty);
			p.add(txtQty);
			
			GAPLENGTH += 30;
		}
		machineryController.setCoinQtyList(txtListCoinQty);
		machineryPanel.add(p);
		
		/*
		Label c5 = new Label(MachineryBuilder.CENT_5);
		c5.setPreferredSize(new Dimension(50, 20));
		c5.setAlignment(Label.RIGHT);
		c5.setFont(new Font("Times New Roman", Font.BOLD, 12));
		
		Label c10 = new Label(MachineryBuilder.CENT_10);
		c10.setPreferredSize(new Dimension(50, 20));
		c10.setAlignment(Label.RIGHT);
		c10.setFont(new Font("Times New Roman", Font.BOLD, 12));
		
		Label c20 = new Label(MachineryBuilder.CENT_20);
		c20.setPreferredSize(new Dimension(50, 20));
		c20.setAlignment(Label.RIGHT);
		c20.setFont(new Font("Times New Roman", Font.BOLD, 12));
		
		Label c50 = new Label(MachineryBuilder.CENT_50);
		c50.setPreferredSize(new Dimension(50, 20));
		c50.setAlignment(Label.RIGHT);
		c50.setFont(new Font("Times New Roman", Font.BOLD, 12));
		
		Label c100 = new Label(MachineryBuilder.CENT_100);
		c100.setPreferredSize(new Dimension(50, 20));
		c100.setAlignment(Label.RIGHT);
		c100.setFont(new Font("Times New Roman", Font.BOLD, 12));
		
		Label c5qty = new Label(MachineryBuilder.ZERO);
		c5qty.setPreferredSize(new Dimension(50, 20));
		c5qty.setAlignment(Label.CENTER);
		c5qty.setBackground(Color.BLACK);
		c5qty.setForeground(Color.YELLOW);
		c5qty.setFont(new Font("Monospaced", Font.PLAIN, 12));
		
		Label c10qty = new Label(MachineryBuilder.ZERO);
		c10qty.setPreferredSize(new Dimension(50, 20));
		c10qty.setAlignment(Label.CENTER);
		c10qty.setBackground(Color.BLACK);
		c10qty.setForeground(Color.YELLOW);
		c10qty.setFont(new Font("Monospaced", Font.PLAIN, 12));
		
		Label c20qty = new Label(MachineryBuilder.ZERO);
		c20qty.setPreferredSize(new Dimension(50, 20));
		c20qty.setAlignment(Label.CENTER);
		c20qty.setBackground(Color.BLACK);
		c20qty.setForeground(Color.YELLOW);
		c20qty.setFont(new Font("Monospaced", Font.PLAIN, 12));
		
		Label c50qty = new Label(MachineryBuilder.ZERO);
		c50qty.setPreferredSize(new Dimension(50, 20));
		c50qty.setAlignment(Label.CENTER);
		c50qty.setBackground(Color.BLACK);
		c50qty.setForeground(Color.YELLOW);
		c50qty.setFont(new Font("Monospaced", Font.PLAIN, 12));
		
		Label c100qty = new Label(MachineryBuilder.ZERO);
		c100qty.setPreferredSize(new Dimension(50, 20));
		c100qty.setAlignment(Label.CENTER);
		c100qty.setBackground(Color.BLACK);
		c100qty.setForeground(Color.YELLOW);
		c100qty.setFont(new Font("Monospaced", Font.PLAIN, 12));
		
		Insets insets = p.getInsets();
		Dimension d = quantityCoin.getPreferredSize();
		quantityCoin.setBounds(1 + insets.left, 0 + insets.top, d.width, d.height);
		
		d = c5.getPreferredSize();
		c5.setBounds(120 + insets.left, 25 + insets.top, d.width, d.height);
		
		d = c5qty.getPreferredSize();
		c5qty.setBounds(180 + insets.left, 25 + insets.top, d.width, d.height);
		
		d = c10.getPreferredSize();
		c10.setBounds(120 + insets.left, 55 + insets.top, d.width, d.height);
		
		d = c10qty.getPreferredSize();
		c10qty.setBounds(180 + insets.left, 55 + insets.top, d.width, d.height);
		
		d = c20.getPreferredSize();
		c20.setBounds(120 + insets.left, 85 + insets.top, d.width, d.height);
		
		d = c20qty.getPreferredSize();
		c20qty.setBounds(180 + insets.left, 85 + insets.top, d.width, d.height);
		
		d = c50.getPreferredSize();
		c50.setBounds(120 + insets.left, 115 + insets.top, d.width, d.height);
		
		d = c50qty.getPreferredSize();
		c50qty.setBounds(180 + insets.left, 115 + insets.top, d.width, d.height);
		
		d = c100.getPreferredSize();
		c100.setBounds(120 + insets.left, 145 + insets.top, d.width, d.height);
		
		d = c100qty.getPreferredSize();
		c100qty.setBounds(180 + insets.left, 145 + insets.top, d.width, d.height);
		
		p.add(quantityCoin);
		p.add(c5);
		p.add(c10);
		p.add(c20);
		p.add(c50);
		p.add(c100);
		p.add(c5qty);
		p.add(c10qty);
		p.add(c20qty);
		p.add(c50qty);
		p.add(c100qty);
		
		machineryPanel.setFiveCentCount(c5qty);
		machineryPanel.setTenCentCount(c10qty);
		machineryPanel.setTwentyCentCount(c20qty);
		machineryPanel.setFiftyCentCount(c50qty);
		machineryPanel.setOneDollarCount(c100qty);
		machineryPanel.add(p);*/
	}
	
	public void buildDisplayPanel(){
		int GAPLENGTH = 0;
		Panel p = new Panel();
		p.setPreferredSize(new Dimension(Constant.PANEL_WIDTH, 225));
		p.setBackground(Constant.DEFAULT_BACKGROUND_COLOR);
		//p.setBackground(Color.PINK);
		p.setLayout(null);
		
		Label quantityCan = new Label(MachineryBuilder.QUANTITY_OF_CANS);
		quantityCan.setPreferredSize(new Dimension(150, 20));
		quantityCan.setFont(new Font("Times New Roman", Font.BOLD, 12));
		
		Insets insets = p.getInsets();
		Dimension d = quantityCan.getPreferredSize();
		quantityCan.setBounds(1 + insets.left, 0 + insets.top, d.width, d.height);
		
		p.add(quantityCan);
		
		
		DrinksStore drinkStore = DrinksStore.getInstance();
		txtListDrinkQty = new TextField[drinkStore.getStoreSize()];
		for(int i =0;i<drinkStore.getStoreSize();i++){
			StoreItem item = drinkStore.getItem(i);
			StoreObject obj = item.getContent();
			
			Label lblBrand = new Label(obj.getName());
			lblBrand.setName(obj.getClass().getName()+ String.valueOf(i));
			lblBrand.setPreferredSize(new Dimension(150, 20));
			lblBrand.setAlignment(Label.RIGHT);
			lblBrand.setFont(new Font("Times New Roman", Font.BOLD, 12));
			
			/*Label lblqty = new Label(String.valueOf(item.getQuantity()));
			lblqty.setName(item.getClass().getName()+String.valueOf(i));
			lblqty.setPreferredSize(new Dimension(100, 20));
			lblqty.setAlignment(Label.CENTER);
			lblqty.setBackground(Color.BLACK);
			lblqty.setForeground(Color.YELLOW);
			lblqty.setFont(new Font("Monospaced", Font.PLAIN, 12));*/
			
			TextField txtQty = new TextField(String.valueOf(item.getQuantity()));
			txtQty.setName(item.getClass().getName()+String.valueOf(i));
			txtQty.setPreferredSize(new Dimension(100, 20));
			txtQty.setBackground(Color.BLACK);
			txtQty.setForeground(Color.YELLOW);
			txtQty.setFont(new Font("Monospaced", Font.PLAIN, 12));
			//txtQty.addActionListener(new StoreViewerListener(1,i,Integer.parseInt(txtQty.getText())));
			txtQty.addTextListener(new StoreViewerListener(1,i,item.getQuantity()));
			//txtQty.addPropertyChangeListener(new StoreViewerListener(1,i,Integer.parseInt(txtQty.getText())));
			
			d = lblBrand.getPreferredSize();
			lblBrand.setBounds(20 + insets.left, 25 + GAPLENGTH + insets.top, d.width, d.height);
			
			/*d = lblqty.getPreferredSize();
			lblqty.setBounds(180 + insets.left, 25 + GAPLENGTH + insets.top, d.width, d.height);*/
			d = txtQty.getPreferredSize();
			txtQty.setBounds(180 + insets.left, 25 + GAPLENGTH + insets.top, d.width, d.height);
			
			txtListDrinkQty[i] = txtQty;
			
			p.add(lblBrand);
			/*p.add(lblqty);*/
			p.add(txtQty);
			
			GAPLENGTH += 30;
		}
		machineryController.setDrinkQtyList(txtListDrinkQty);
		machineryPanel.add(p);
		/*Label brand1 = new Label("");
		brand1.setPreferredSize(new Dimension(150, 20));
		brand1.setAlignment(Label.RIGHT);
		brand1.setFont(new Font("Times New Roman", Font.BOLD, 12));
		
		Label qty1 = new Label(MachineryBuilder.ZERO);
		qty1.setPreferredSize(new Dimension(100, 20));
		qty1.setAlignment(Label.CENTER);
		qty1.setBackground(Color.BLACK);
		qty1.setForeground(Color.YELLOW);
		qty1.setFont(new Font("Monospaced", Font.PLAIN, 12));
		
		Label brand2 = new Label("");
		brand2.setPreferredSize(new Dimension(150, 20));
		brand2.setAlignment(Label.RIGHT);
		brand2.setFont(new Font("Times New Roman", Font.BOLD, 12));
		
		Label qty2 = new Label(MachineryBuilder.ZERO);
		qty2.setPreferredSize(new Dimension(100, 20));
		qty2.setAlignment(Label.CENTER);
		qty2.setBackground(Color.BLACK);
		qty2.setForeground(Color.YELLOW);
		qty2.setFont(new Font("Monospaced", Font.PLAIN, 12));
		
		Label brand3 = new Label("");
		brand3.setPreferredSize(new Dimension(150, 20));
		brand3.setAlignment(Label.RIGHT);
		brand3.setFont(new Font("Times New Roman", Font.BOLD, 12));
		
		Label qty3 = new Label(MachineryBuilder.ZERO);
		qty3.setPreferredSize(new Dimension(100, 20));
		qty3.setAlignment(Label.CENTER);
		qty3.setBackground(Color.BLACK);
		qty3.setForeground(Color.YELLOW);
		qty3.setFont(new Font("Monospaced", Font.PLAIN, 12));
		
		Label brand4 = new Label("");
		brand4.setPreferredSize(new Dimension(150, 20));
		brand4.setAlignment(Label.RIGHT);
		brand4.setFont(new Font("Times New Roman", Font.BOLD, 12));
		
		Label qty4 = new Label(MachineryBuilder.ZERO);
		qty4.setPreferredSize(new Dimension(100, 20));
		qty4.setAlignment(Label.CENTER);
		qty4.setBackground(Color.BLACK);
		qty4.setForeground(Color.YELLOW);
		qty4.setFont(new Font("Monospaced", Font.PLAIN, 12));
		
		Label brand5 = new Label("");
		brand5.setPreferredSize(new Dimension(150, 20));
		brand5.setAlignment(Label.RIGHT);
		brand5.setFont(new Font("Times New Roman", Font.BOLD, 12));
		
		Label qty5 = new Label(MachineryBuilder.ZERO);
		qty5.setPreferredSize(new Dimension(100, 20));
		qty5.setAlignment(Label.CENTER);
		qty5.setBackground(Color.BLACK);
		qty5.setForeground(Color.YELLOW);
		qty5.setFont(new Font("Monospaced", Font.PLAIN, 12));
		
		Insets insets = p.getInsets();
		Dimension d = quantityCan.getPreferredSize();
		quantityCan.setBounds(1 + insets.left, 0 + insets.top, d.width, d.height);
		
		d = brand1.getPreferredSize();
		brand1.setBounds(20 + insets.left, 25 + insets.top, d.width, d.height);
		
		d = qty1.getPreferredSize();
		qty1.setBounds(180 + insets.left, 25 + insets.top, d.width, d.height);
		
		d = brand2.getPreferredSize();
		brand2.setBounds(20 + insets.left, 55 + insets.top, d.width, d.height);
		
		d = qty2.getPreferredSize();
		qty2.setBounds(180 + insets.left, 55 + insets.top, d.width, d.height);
		
		d = brand3.getPreferredSize();
		brand3.setBounds(20 + insets.left, 85 + insets.top, d.width, d.height);
		
		d = qty3.getPreferredSize();
		qty3.setBounds(180 + insets.left, 85 + insets.top, d.width, d.height);
		
		d = brand4.getPreferredSize();
		brand4.setBounds(20 + insets.left, 115 + insets.top, d.width, d.height);
		
		d = qty4.getPreferredSize();
		qty4.setBounds(180 + insets.left, 115 + insets.top, d.width, d.height);
		
		d = brand5.getPreferredSize();
		brand5.setBounds(20 + insets.left, 145 + insets.top, d.width, d.height);
		
		d = qty5.getPreferredSize();
		qty5.setBounds(180 + insets.left, 145 + insets.top, d.width, d.height);
		
		p.add(quantityCan);
		p.add(brand1);
		p.add(brand2);
		p.add(brand3);
		p.add(brand4);
		p.add(brand5);
		p.add(qty1);
		p.add(qty2);
		p.add(qty3);
		p.add(qty4);
		p.add(qty5);
		
		Label[] brands = new Label[]{brand1, brand2, brand3, brand4, brand5};
		Label[] quantities = new Label[]{qty1, qty2, qty3, qty4, qty5};
		
		machineryPanel.setBrands(brands);
		machineryPanel.setQuantities(quantities);
		machineryPanel.add(p);		*/
	}
	
	public void buildStatusPanel(){
		Panel p = new Panel();
		p.setPreferredSize(new Dimension(Constant.PANEL_WIDTH, 70));
		p.setBackground(Constant.DEFAULT_BACKGROUND_COLOR);
		
		/*
		Checkbox doorStatus = new Checkbox(MachineryBuilder.DOOR_STATUS);
		doorStatus.setState(true);
		
		p.add(doorStatus);
		*/
		p.setLayout(null);
		Checkbox doorStatus = new Checkbox();
		doorStatus.setState(true);
		
		Label doorStatusMessage = new Label(MachineryBuilder.DOOR_STATUS);
		doorStatusMessage.setPreferredSize(new Dimension(100, 20));
		doorStatusMessage.setFont(new Font("Times New Roman", Font.BOLD, 12));
		//doorStatusMessage.setBackground(Color.RED);
		
		Insets insets = p.getInsets();
		//Dimension d = doorStatus.getPreferredSize();
		doorStatus.setBounds(120 + insets.left, 0 + insets.top, 15, 20);
		//doorStatus.setBackground(Color.GREEN);
		//System.out.println("width = " + d.width);
		//System.out.println("height = " + d.height);
		
		Dimension d = doorStatusMessage.getPreferredSize();
		doorStatusMessage.setBounds(140 + insets.left, 0 + insets.top, d.width, d.height);
		
		p.add(doorStatus);
		p.add(doorStatusMessage);
		
		machineryPanel.setTitlePanel(p);
		machineryPanel.add(p);
	}
	
	public Panel getResult(){
		return this.machineryPanel;
	}
	
	public static void main(String args[]){
		Frame f = new Frame();
		//f.setPreferredSize(new Dimension(300, 600));
		f.setTitle("VMCS - Machinery Panel");
		f.setSize(new Dimension(Constant.PANEL_WIDTH, 600));
		f.setResizable(false);
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		
		UIBuilder uib = BuilderFactory.getInstance().getUIBuilder("machineryUIBuilder");
		uib.buildTitlePanel();
		uib.buildInputPanel();
		uib.buildDisplayPanel();
		uib.buildStatusPanel();
		f.add(uib.getResult());
		f.setVisible(true);
	}
}
